﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;




namespace ASoft.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class TreeViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the TreeViewModel class.
        /// </summary>
        public TreeViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
        }

        #region Items

        ObservableCollection<TreeItemViewModel> _Items;
        public ObservableCollection<TreeItemViewModel> Items
        {
            get
            {
                if (_Items == null)
                {
                    _Items = new ObservableCollection<TreeItemViewModel>();
                }
                return _Items;
            }
        }


        #endregion


        #region FirstSelectedItem

        /// <summary>
        /// The <see cref="FirstSelectedItem" /> property's name.
        /// </summary>
        public const string FirstSelectedItemPropertyName = "FirstSelectedItem";

        private TreeItemViewModel _FirstSelectedItem = null;

        /// <summary>
        /// Gets the FirstSelectedItem property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public TreeItemViewModel FirstSelectedItem
        {
            get
            {
                return _FirstSelectedItem;
            }

            internal set
            {
                if (_FirstSelectedItem == value)
                {
                    return;
                }

                var oldValue = _FirstSelectedItem;
                _FirstSelectedItem = value;
                RaisePropertyChanged(FirstSelectedItemPropertyName);
            }
        }

        #endregion


        #region SelectedItems

        ObservableCollection<TreeItemViewModel> _SelectedItems;
        public ObservableCollection<TreeItemViewModel> SelectedItems
        {
            get
            {
                if (_SelectedItems == null)
                {
                    _SelectedItems = new ObservableCollection<TreeItemViewModel>();
                }
                return _SelectedItems;
            }
        }

        internal void ReloadSelectedItems()
        {
            var list = new List<string>();

            SelectedItems.Clear();
            LoadSelectedItems(Items);
            FirstSelectedItem = SelectedItems.FirstOrDefault();
        }

        void LoadSelectedItems(ObservableCollection<TreeItemViewModel> items)
        {
            foreach (var item in items)
            {
                if (item.IsSelected)
                {
                    SelectedItems.Add(item);
                }
                if (item.Items.Count > 0)
                {
                    LoadSelectedItems(item.Items);
                }
            }
        }

        #endregion

        public TreeItemViewModel NewItem()
        {
            return new TreeItemViewModel(this);
        }

        public bool IsChangedChild { get; set; }

    }
}